package com.hxzy.controller.seckill;

import com.hxzy.common.resp.R;
import com.hxzy.controller.seckill.req.SeckillReq;
import com.hxzy.service.SeckillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author wy
 * @Description redis分布式锁
 * @Date 2023/9/11 11:31
 * @Version 1.0
 */
@RestController
public class RedisStringNXController {

    /**
     * redis来实布式锁
     */
    @Qualifier(value = "redisSeckillServiceImpl")
    @Autowired
    private SeckillService seckillService;


    /**
     * 数据库悲观锁
     */
    @Autowired
    private SeckillService storeCountServiceImpl;


    /**
     * 数据库乐观锁
     */
    @Autowired
    private SeckillService storeCountVersionServiceImpl;


    /**
     * redis分布式锁
     * @param req
     * @return
     */
    @PostMapping(value = "/seckill/redis")
    public R seckill(@RequestBody  SeckillReq req){
        Long orderId = this.seckillService.seckill(req);
        return R.okHasData(orderId);

    }

    /**
     * 使用数据库悲观锁
     * @param req
     * @return
     */
    @PostMapping(value = "/seckill/db/lock")
    public R seckillDBLock(@RequestBody  SeckillReq req){
        Long orderId = this.storeCountServiceImpl.seckill(req);
        return R.okHasData(orderId);
    }

    /**
     * 使用数据库乐观锁
     * @param req
     * @return
     */
    @PostMapping(value = "/seckill/db/version")
    public R seckillDBVersion(@RequestBody  SeckillReq req){
        Long orderId = this.storeCountVersionServiceImpl.seckill(req);
        return R.okHasData(orderId);
    }

}
